package com.repair.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.client.PlatformClient;
import com.repair.client.dto.AdminInfoDto;
import com.repair.dao.AreaInfoMapper;
import com.repair.dto.AreaAddDto;
import com.repair.dto.AreaInfoDto;
import com.repair.entity.AreaInfo;
import com.repair.exception.RepairInfoException;
import com.repair.form.AreaUpdateForm;
import com.repair.param.AreaAddParam;
import com.repair.param.AreaFindParam;
import com.repair.param.AreaUpdateParam;
import com.repair.service.AreaInfoService;
import com.repair.util.Result;
import com.repair.util.StateDate;
import jdk.nashorn.internal.runtime.regexp.joni.WarnCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 维修区域表 服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@Service
public class AreaInfoServiceImpl extends ServiceImpl<AreaInfoMapper, AreaInfo> implements AreaInfoService {
    @Autowired
    private AreaInfoMapper areaInfoMapper;
    @Autowired
    private PlatformClient platformClient;

    @Override
    public AreaAddDto addArea(AreaAddParam param,Integer adminId) {
        //新增区域信息:区域名称不能重复
        QueryWrapper<AreaInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("area_name",param.getAreaName());
        AreaInfo areaInfo = areaInfoMapper.selectOne(wrapper);
        if (areaInfo !=null) throw new RepairInfoException(500,"区域名称不能重复");
        //可以进行新增区域,需要调用其他服务的接口查询管理员信息
        AreaInfo area = BeanUtil.toBean(param, AreaInfo.class);
        Result<AdminInfoDto> result = platformClient.findAdminById(adminId);
        if (result.getCode()!=200){
            throw new RepairInfoException(result.getCode(), result.getMessage());
        }
        area.setCreatePerson(result.getData().getAdminUser());
        area.setState(StateDate.AREA_STATE_SHOW);
        area.setCreateTime(LocalDateTime.now().toString());
        area.setUpdatePerson("");
//        area.setUpdateTime("");
        areaInfoMapper.insert(area);
        AreaAddDto dto = BeanUtil.toBean(area, AreaAddDto.class);
        return dto;
    }

    @Override
    public AreaInfo updateArea(AreaUpdateParam param, Integer updateId) {
        //先判断维修区域id是否存在
        AreaInfo areaInfo = areaInfoMapper.selectById(param.getAreaId());
        if (areaInfo==null) throw new RepairInfoException(500,"维修区域不存在");
        //修改维修区域信息，查询出修改人的信息
        Result<AdminInfoDto> result = platformClient.findAdminById(updateId);
        if (result.getCode()!=200){
            throw new RepairInfoException(result.getCode(), result.getMessage());
        }
        areaInfo.setAreaName(param.getAreaName());
        areaInfo.setUpdatePerson(result.getData().getAdminUser());
        areaInfo.setUpdateTime(LocalDateTime.now().toString());
        areaInfoMapper.updateById(areaInfo);
        return areaInfo;
    }

    @Override
    public void delArea(Integer areaId, Integer updateId) {
        //先判断维修区域id是否存在
        AreaInfo info = areaInfoMapper.selectById(areaId);
        if (info==null) throw new RepairInfoException(500,"维修区域不存在");
        //可以进行删除，修改维修区域状态，查询出修改人的信息
        Result<AdminInfoDto> result = platformClient.findAdminById(updateId);
        if (result.getCode()!=200){
            throw new RepairInfoException(result.getCode(), result.getMessage());
        }
        info.setState(StateDate.AREA_STATE_DELETE);
        info.setUpdatePerson(result.getData().getAdminUser());
        info.setUpdateTime(LocalDateTime.now().toString());
        areaInfoMapper.updateById(info);

    }

    @Override
    public AreaInfoDto findAreaInfoById(Integer areaId) {
        AreaInfo areaInfo = areaInfoMapper.selectById(areaId);
        if (areaInfo==null) throw new RepairInfoException(500,"维修区域不存在");
        return BeanUtil.toBean(areaInfo,AreaInfoDto.class);
    }

    @Override
    public List<AreaInfo> findAreaInfoByCondition(AreaFindParam param) {
        //根据条件查询区域信息
        QueryWrapper<AreaInfo> wrapper = new QueryWrapper<>();
        if (param.getAreaName()!=null && param.getAreaName()!=""){
            wrapper.like("area_name",param.getAreaName());
        }
        if (param.getCreatePerson()!=null && param.getCreatePerson()!=""){
            wrapper.like("create_person",param.getCreatePerson());
        }
        if (param.getUpdatePerson() != null && param.getUpdatePerson() != "") {
            wrapper.like("update_person",param.getUpdatePerson());
        }
        wrapper.ne("state",StateDate.AREA_STATE_DELETE);
        List<AreaInfo> list = areaInfoMapper.selectList(wrapper);
        return list;
    }
}
